var dayNames = Array("Monday","Tuesday","Wednesday","Thursday","Friday");
var boxWidth=0;
var boxHeight=0;
    function drawSchedule() {
	for(var i in dayNames)
	$("#schedule").append('<div id='+dayNames[i]+' style =  "font-size:20px;border-left:1px solid #999; position: absolute; z-index: 99; height:102%; left:'+ (6+19*i) +'%; top: 0%;  width: 19%">'+dayNames[i]+'</div>');
	
	for(i=0;i<14;i++)
	$("#schedule").append('<div id='+i+' style =  "border-top:1px solid #999; font-size: 20px; position: absolute; z-index: 99; width:102%; left:0%; top: '+(i*7+6)+'%; height:7%;">'+((i+7)%12+1)+'</div>');

    }
var response;
var numClass;
function drawAllClasses(n){
	n=Math.abs(n);
	document.getElementById("classContainer").innerHTML = "";

	var c = getRandColor();
	document.getElementById("controls").innerHTML="<h3>you are viewing answer set "+Math.abs((numClass%response.length)+1)+' of '+(response.length>100 ? "100+" :response.length)+'</h3> <div id="hide3"><input type="button" value="last schedule" onclick="drawAllClasses(numClass--);"></div><div id="hide2"><input type="button" value="next schedule" onclick="drawAllClasses(numClass++);"></div> <h3>uniques involved</h3>';

	var thisunique="";
	var thisSet=response[n%response.length];
	for(i in thisSet){
		if(response[0][i][0]!=thisunique){
			document.getElementById("controls").innerHTML=document.getElementById("controls").innerHTML+"<br>"+abbreviation(thisSet[i][0])+" : "+thisSet[i][1];
			thisunique=response[0][i][0];
			c = getRandColor();
		}
	
		DrawClass(thisSet[i][2],thisSet[i][3],thisSet[i][4],thisSet[i][0],c,thisSet[i][1]);
	}
	document.getElementById("controls").innerHTML=document.getElementById("controls").innerHTML+"<br /><input id=\"exportInput\" onFocus=\"clearText(this);\"  onkeypress=\"{if (event.keyCode==13)exportSchedule();}\" Value=\"Enter the number of the schedules you want to export, seperated by commas (e.g. 1,2,4,5)\" />";
	
}
function exportSchedule(){
	xmlhttpPostXls("/cgi-bin/NTFTRunner/xls.py");

}
function xmlhttpPostXls(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'text');
    
	var filename = ''+ new Date().getTime()+'.xls';
	//create download link.
	
    self.xmlHttpReq.send(""+filename+"&list="+document.getElementById("exportInput").value);
	
	var ahref= "<br> <a href=\"/exports/"+filename+"\">Right click, save target as</a>";
	
	document.getElementById("controls").innerHTML=document.getElementById("controls").innerHTML+ahref;	
}

var classid=0;
var ays = Array("m","t","w","th","f");
function getRandColor(){
	return "rgb("+Math.floor(Math.random()*255)+","+Math.floor(Math.random()*255)+","+Math.floor(Math.random()*255)+")";
}

function DrawClass(day,start,end,nam,c,unique){
	if(nam!=undefined && nam!="not found"){
		var x=ays.indexOf(day)*19+6;
		var y=((start/100)-8)*7+6; 
			
		//convert start and end times to a string with colon and am/pm
		//this is in the same format as in xls.py, so im just copy pasting code here.
		var startHour="";
		var startMinute="";
		var finishHour="";
		var finishMinute="";
		
		var result1="";
		var result2="";
		
		if(start.toString().length > 3){
			startHour=start.substring(0,2);
			startMinute=start.substring(2,5);
		}
		else{
			startHour=start.substring(0,1);
			startMinute=start.substring(1,4);
		}
		///////////////////////////////////////////////////
		if(end.toString().length > 3){
			finishHour=end.substring(0,2);
			finishMinute=end.substring(2,5);
		}
		else{
			finishHour=end.substring(0,1);
			finishMinute=end.substring(1,4);
		}
		////////////////////////////////////////////////////////////
		if(parseInt(startHour) > 12){
			result1 = ""+(parseInt(startHour)-12) + ":" + startMinute;
 		}
		else{
			result1 = startHour + ":" + startMinute;
		}
		
		if(parseInt(startHour) >= 12){
			result1+=" PM";
		}
		else{
			result1+=" AM";
		}
		///////////////////////////////////////////////////
		if(parseInt(finishHour) > 12){
			result2 = ""+(parseInt(finishHour)-12) + ":" + finishMinute;
 		}
		else{
			result2 = finishHour + ":" + finishMinute;
		}
		
		if(parseInt(finishHour) >= 12){
			result2+=" PM";
		}
		else{
			result2+=" AM";
		}
		////now just display result 1 and 2
		
		$("#classContainer").append('<div id="big'+(classid)+'" onmouseover="showBig('+(classid)+');" onmouseout="hideBig('+(classid)+');" id="class'+(classid)+'" style="background:'+c+'">Name: '+nam.toUpperCase()+'<br> Unique ID: '+unique+' <br>Time Range: '+result1+'-'+result2+'</div>');
		$("#big"+classid).css({'border': '1px solid #999',                       
		               'position': 'absolute',
					   'word-wrap':'break-word',
			       'border-radius': '5px',
		               'z-index': 150,
			       'font-size': "20px",
				 'text-outline': '1px 1px #fff',
		               'left': x+'%', 'top': y+'%',
		               'width': '40%', 'height': '30%'});
		

		$("#classContainer").append('<div onmouseover="showBig('+(classid)+');" onmouseout="hideBig('+(classid)+');" id="class'+(classid)+'" style="background:'+c+'">'+abbreviation(nam)+'</div>');
		$("#class"+classid).css({				
				'border': '1px solid #999',                       
		               'position': 'absolute',
			       'border-radius': '5px',
		               'z-index': 100,
			       'font-size': "40px",
				 'text-outline': '1px 1px #fff',
		               'left': x+'%', 'top': y+'%',
		               'width': '19%', 'height': 7*(end-start)/100+'%'});		



		document.getElementById("big"+(classid)).style.display = 'none';

		classid++;

	}
}
function showBig(id){
	document.getElementById("big"+id).style.display = 'block';		
}
function hideBig(id) {
	var zxce=window.event||arguments.callee.caller.arguments[0];
	if (!zxcCkEventObj(zxce)) return false;
	document.getElementById("big"+id).style.display = 'none';
}
function zxcCkEventObj(zxce){
	if (!zxce) var zxce=window.event;
	zxce.cancelBubble=true;
	if (zxce.stopPropagation) zxce.stopPropagation();
	var zxceobj=(zxce.relatedTarget)?zxce.relatedTarget:(zxce.type=='mouseout')?zxce.toElement:zxce.fromElement;
	while (zxceobj.parentNode){
	if (zxceobj.id&&(zxceobj.id=='thumbviewer')){ return false; }
	zxceobj=zxceobj.parentNode;
	}
	return true;
}

function abbreviation(string){
	for(var i=4;i<7;i++)
		if("0123456789".indexOf(string[i])==-1)
			return string.substring(0,i).toUpperCase();

	if(string.length>=8)
		return string.substring(0,7).toUppercase();
	return string;
}

